package com.xxx.proj.controller;

import com.xxx.proj.entity.Result;
import com.xxx.proj.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.tools.DiagnosticListener;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        //把文件存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的,从而防止文件覆盖
        String filename= UUID.randomUUID().toString()+
                originalFilename.substring(originalFilename.lastIndexOf("."));
        //file.transferTo(new File("C:\\Users\\86137\\Desktop\\springboot\\file\\"+filename));
        String url = AliOssUtil.uplocalFile(filename,file.getInputStream());
        return Result.success(url);
    }
}
